package com.ytkj.digitalConstruction.service.constructionprocess;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ytkj.digitalConstruction.dao.constructionprocess.ConstructionSucceedMapper;
import com.ytkj.digitalConstruction.entity.constructionprocess.ConstructionSucceed;
import com.ytkj.digitalConstruction.entity.user.User;
import com.ytkj.digitalConstruction.enums.construction.ConstructionResultEnum;
import com.ytkj.digitalConstruction.exception.CustomException;
import com.ytkj.digitalConstruction.httpbean.constructionprocess.reqbean.HistoryQueryBean;
import com.ytkj.digitalConstruction.httpbean.constructionprocess.resbean.SuccedAboutFlowBase;
import com.ytkj.digitalConstruction.httpbean.constructionprocess.resbean.TodayAndHistory;

/**
 * 交接单Service
 * 
 * @author 宁煜
 *
 */
@Service
@Transactional
public class ConstructionSucceedService {

	@Autowired
	private ConstructionSucceedMapper constructionSucceedMapper;
	
	@Autowired
	private ConstructionPlanService constructionPlanService;
	
	/**
	 * @Description: 
	 * @Title: addConstructionSucceed 
	 * @param @param constructionSucceed
	 * @param @param user    设定文件 
	 * @return void    返回类型 
	 * @throws
	 */
	public ConstructionSucceed addConstructionSucceed(ConstructionSucceed constructionSucceed, User user) {
		
		// 验证指定流程下是否已经有交接单信息
		if(getSucceedByFlowId(constructionSucceed.getFlowId()) != null) {
			throw new CustomException(ConstructionResultEnum.EXSIST_SUCCEED);
		}
		
		Date date = new Date();
		constructionSucceed.setId(null);
		constructionSucceed.setDate(date);
		constructionSucceed.setCreateDate(date);
		constructionSucceed.setUpdateDate(date);
		constructionSucceed.setCreateBy(user.getUserId());
		constructionSucceed.setUpdateBy(user.getUserId());
		
		constructionSucceedMapper.insertSelective(constructionSucceed);
		ConstructionUtilService.isAddSuccess(constructionSucceed.getId());
		return constructionSucceed;
	}
	
	/**
	 * @Description: 根据流程查询交接单信息
	 * @Title: getSucceedByFlowId 
	 * @param @param flowId
	 * @param @return    设定文件 
	 * @return ConstructionSucceed    返回类型 
	 * @throws
	 */
	public ConstructionSucceed getSucceedByFlowId(Integer flowId) {
		return constructionSucceedMapper.selectByFlowId(flowId);
	}
	
	/**
	 * @Description: 根据指定项目查询今日及历史
	 * @Title: getTodayAndHistory 
	 * @param @return    设定文件 
	 * @return TodayAndHistory<ConstructionSucceed>    返回类型 
	 * @throws
	 */
	public TodayAndHistory<ConstructionSucceed> getTodayAndHistory(HistoryQueryBean historyQueryBean) {
		TodayAndHistory<ConstructionSucceed> todayAndHistory = new TodayAndHistory<>();
		todayAndHistory.setTodayInfo(constructionSucceedMapper.showToday(historyQueryBean.getId()));
		todayAndHistory.setHistoryInfo(constructionSucceedMapper.showHistory(historyQueryBean));
		return todayAndHistory;
	}
	
	/**
	 * @Description: 修改信息
	 * @Title: updateConstructionSucceed 
	 * @param @param constructionSucceed
	 * @param @param user    设定文件 
	 * @return void    返回类型 
	 * @throws
	 */
	public ConstructionSucceed updateConstructionSucceed(ConstructionSucceed constructionSucceed, User user) {
		constructionSucceed.setUpdateBy(user.getUserId());
		constructionSucceed.setDate(new Date());
		ConstructionUtilService.isUpdateSuccess(constructionSucceedMapper.updateByPrimaryKeySelective(constructionSucceed));
		return constructionSucceedMapper.selectByPrimaryKey(constructionSucceed.getId());
	}
	
	/**
	 * @Description: 修改或者新增
	 * @Title: updateOrAdd 
	 * @param @param constructionSucceed
	 * @param @param user    设定文件 
	 * @return void    返回类型 
	 * @throws
	 */
	public ConstructionSucceed updateOrAdd(ConstructionSucceed constructionSucceed, User user) {
		if(constructionSucceed.getId()==null) {
			return addConstructionSucceed(constructionSucceed, user);
		}else {
			return updateConstructionSucceed(constructionSucceed, user);
		}
	}
	
	/**
	 * @Description: 根据指定的编号获取交接单信息
	 * @Title: showInfoById 
	 * @param @param id
	 * @param @return    设定文件 
	 * @return ConstructionSucceed    返回类型 
	 * @throws
	 */
	public SuccedAboutFlowBase<ConstructionSucceed> showInfoById(Integer id) {
		SuccedAboutFlowBase<ConstructionSucceed> aboutFlowBase = new SuccedAboutFlowBase<>();
		aboutFlowBase.setInfoBase(constructionSucceedMapper.selectByPrimaryKey(id));
		if(aboutFlowBase.getInfoBase() != null) {
			aboutFlowBase.setPlanContentToday(constructionPlanService.getTodayContentByFlowId(aboutFlowBase.getInfoBase().getFlowId()));
		}
		return aboutFlowBase;
	}
	
	
}
